<?php
/** UTF-8
 * Concentré Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Concentre
 * @package    Concentre_Archive
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version	   $ID$ r$Rev: 11 $ $Author: scramatte $ $Date: 2009-03-18 11:26:09 +0100 (Wed, 18 Mar 2009) $ 
 * @author	   S�bastien Cramattte <scramatte@zensoluciones.com>
 * @author	   Alban Lopez <alban.lopez+archive@gmail.com>
 * @see		   Based on EasyArchive <http://www.phpclasses.org/browse/package/4239.html>
 */


/**
 * @see Concentre_Archive_Gzip
 */
require 'Concentre/Archive/Gzip.php';

/**
 * @see Concentre_Archive_Tar
 */
require 'Concentre/Archive/Tar.php';

/**
 * @see Concentre_Archive_Zip
 */
require 'Concentre/Archive/Zip.php';

/**
 * @see Concentre_Archive_Bzip2
 */
require 'Concentre/Archive/Bzip2.php';


/**
 * @category   Concentre
 * @package    Concentre_Archive
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Concentre_Archive
{

	protected $_extensions = array (
		'.zip'		=>'Zip',
		'.tar'		=>'Tar',
		'.gz'		=>'Gzip',
		'.gzip'		=>'Gzip',
		'.bzip'		=>'Gzip',
		'.bz'		=>'Bzip2',
		'.bzip2'	=>'Bzip2',
		'.bz2'		=>'Bzip2',
		'.tgz'		=>'Gzip',
		'.tgzip'	=>'Gzip',
		'.tbzip'	=>'Bzip2',
		'.tbz'		=>'Bzip2',
		'.tbzip2'	=>'Bzip2',
		'.tbz2'		=>'Bzip2',
	);
	
	/*
	function make ($src, $name="Archive.tgz", $returnFile=true)
	{
		$ext = '.' . pathinfo ($name, PATHINFO_EXTENSION);
		$className = 'Concentre_Archive_' . $this->_extensions[$ext];
		
		if (!Class_exists($className)) { 
			throw new Concentre_Archive_Exception('Unknown file extension.');	
		}
				
		$archive = new $className;
		$result = $archive->make($src);

		if ($returnFile) {
			file_put_contents($dest, $result);
		}
		
		return $result;
		
		switch (  ) {
			case 'zip':

			$zip = new Concentre_Archive_Zip;
			
			if ($returnFile) {
				$result = $zip->makeZip($src, $dest);
			} else {
			
				$tmpZip = './'.md5(serialize($src)).'.zip';
				$result = $zip->make($src, $tmpZip);
				$result = file_get_contents($tmpZip);
				unlink($tmpZip);
			}
			return $result;
			break;
			
			case 'tar':
	
				$tar = new Concentre_Archive_Tar;
				$src = $tar->make($src);

				$gzip = new Concentre_Archive_Gzip;
				$src = $gzip->make($src);	
	
			break;
			
			case 'bz':
			
	}

	function infos ($src, $data=false)
	{
		$ext = '.'.pathinfo ($src, PATHINFO_EXTENSION);
		$className = 'Concentre_Archive_' . $this->_extensions[$ext];

		if (!Class_exists($className)) { 
			throw new Concentre_Archive_Exception('Unknown file extension.');	
		}

		$archive = new $className;
		$result = $archive->infos($src, true);


		return $result;
		
		
		/*
		foreach ($this->WathArchive as $key=>$val)
			if (stripos($ext, $key)!==false) $comp=$val;
		if ($comp == 'zip')
		{
			$zip = new zip;
			$zipresult = $zip->infosZip($src, $data);
			$result ['Items'] = count($zipresult);
			foreach($zipresult as $key=>$val)
				$result['UnCompSize'] += $zipresult[$key]['UnCompSize'];
			$result['Size']=filesize($src);
			$result['Ratio'] = $result['UnCompSize'] ? round(100 - $result['Size'] / $result['UnCompSize']*100, 1) : false;
		}
		elseif (strlen($comp)>1)
		{
			$tar = new tar;
			if ($comp == 'bz')
			{
				$bzip2 = new bzip2;
				$result = $bzip2->infosBzip2($src, true);
				$src=$result['Data'];
			}
			elseif ($comp == 'gz')
			{
				$gzip = new gzip;
				$result = $gzip->infosGzip($src, true);
				$src=$result['Data'];
			}
			
			if ($tar->is_tar($src) || is_file($src))
			{
				$tarresult = $tar->infosTar($src, false);
				$result ['Items'] = count($tarresult);
				$result ['UnCompSize'] = 0;
				if (empty($result['Size']))
					$result['Size']=is_file($src)?filesize($src):strlen($src);
				foreach($tarresult as $key=>$val)
					$result['UnCompSize'] += $tarresult[$key]['size'];
				$result['Ratio'] = $result['UnCompSize'] ? round(100 - $result['Size'] / $result['UnCompSize']*100, 1) : false;
				
			}
			if (!$data) unset($result['Data']);
		}
		else return false;
		return array('Items'=>$result['Items'], 'UnCompSize'=>$result['UnCompSize'], 'Size'=>$result['Size'], 'Ratio'=>$result['Ratio'],);
		
	
	}
	*/

    /**
     * Make archive
     *   
     * @param  DirectoryIterator $src
     * @param  string $dest
     * @return 
     */	
	public static function make($src, $dest)
	{
		$parts = pathinfo ($dest);
		$ext = $parts['extension'];
	
		$className = 'Concentre_Archive_' . $this->_extensions[$ext];

		if (!Class_exists($className)) { 
			throw new Concentre_Archive_Exception('Unknown file extension.');	
		}
	
		$archive = new $className;
		//$result = $archive->make($src, $dest);

	}

    /**
     * Extract archive
     *   
     * @param  string $src
     * @param  string $dest
     * @return binary|string|boolean
     */		
	public static function extract ($src, $dest=null)
	{
		
		$parts = pathinfo ($src);
		$ext = $parts['extension'];
		
		
		if (!$dest) {
			$dest = $parts['dirname']. DIRECTORY_SEPARATOR . $parts['basename'];
			if (!is_dir($dest) ) {
				mkdir($dest, '0755');
			}
		}
		
		

		$className = 'Concentre_Archive_' . $this->_extensions[$ext];

		if (!Class_exists($className)) { 
			throw new Concentre_Archive_Exception('Unknown file extension.');	
		}
	
		$archive = new $className;
		$result = $archive->extract($src, $dest);
	
		return $result;
		
	}

    /**
     * Get infos
     * 
     * @param  string $src
     * @return object
     */	
	public static function infos($src)
	{
		$ext = pathinfo ($src, PATHINFO_EXTENSION);
		$className = 'Concentre_Archive_' . $this->_extensions[$ext];

		if (!Class_exists($className)) { 
			throw new Concentre_Archive_Exception('Unknown file extension.');	
		}
	
		$archive = new $className;
		$infos = $archive->infos($src);
	
	}	
}
